To implement these 2-D analysis techniques in R, we recommend using packages such as tidyverse, tableone, gplots, and DT for creating contingency tables, heatmaps, and interactive tables. Below, we illustrate how to construct and interpret each of these visualizations using sample data.
A contingency table helps summarize the frequency of two categorical variables. In this example, we cross-tabulate intervention type and region to identify patterns in research distribution.
# Load required libraries
library(tidyverse)
contingency <- Outcome %>%
group_by(System_C,System_T) %>%
count()
# Create contingency table
contingency_table <- table(Outcome$Crop_FAO_C, Outcome$System_C)
# Display as a data frame
as.data.frame.matrix(contingency_table)## Abandoned Monoculture Natural Simplified other
## 1 - CEREALS AND CEREAL PRODUCTS 0 841 0 55
## 10 - SPICES 0 8 0 0
## 11 - FODDER CROPS AND PRODUCTS 0 29 26 71
## 12 - STIMULANT CROPS AND DERIVED PRODUCTS 0 194 0 194
## 2 - ROOTS AND TUBERS AND DERIVED PRODUCTS 0 28 0 5
## 3 - SUGAR CROPS AND SWEETENERS AND DERIVED PRODUCTS 0 2 0 0
## 4 - PULSES AND DERIVED PRODUCTS 0 4 0 0
## 5 - NUTS AND DERIVED PRODUCTS 0 18 0 0
## 6 - OIL-BEARING CROPS AND DERIVED PRODUCTS 0 222 0 33
## 7 - VEGETABLES AND DERIVED PRODUCTS 0 560 0 23
## 8 - FRUITS AND DERIVED PRODUCTS 0 588 0 60
## 9 - FIBRES OF VEGETAL AND ANIMAL ORIGIN 0 305 0 0
## NA 25 0 673 0
## Other or nd 8 70 0 34
Heatmaps are excellent for visualizing interactions or intensities. This example uses a heatmap to display intervention effect sizes across different farm types.
# Load required libraries
library(ggplot2)
# Create sample data
heatmap_data <- Outcome %>%
group_by(System_C,System_T) %>%
count()
# Create heatmap
ggplot(heatmap_data, aes(x = System_C, y = System_T, fill = n)) +
geom_tile() +
scale_fill_gradient2(low = "red", high = "blue", mid = "white", midpoint = 0) +
theme_minimal() +
labs(title = "Number of studies far various control X treatment combination",
fill = "Number of studies")
Bubble plots are useful for showing interactions between different categories and the size of intervention effects. They encode information through the position, size, and color of the bubbles, making it easy to see patterns and differences. For example, a bubble plot can show intervention effects across various farm types, with larger bubbles indicating stronger effects. This helps highlight which interventions work best in different contexts.
# Load required libraries
library(ggplot2)
# Create sample data
bubble_data <- Outcome %>%
group_by(System_C,System_T) %>%
count()
# Créez un graphique à bulles
bubble_plot <- ggplot(bubble_data, aes(x = System_C, y = System_T)) +
geom_point(aes(size = n, fill = n, color = n), alpha = 0.75, shape = 21) +
# Échelle personnalisée pour la taille des bulles
scale_size_continuous(
limits = c(0.000001, 100), # Limites personnalisées
range = c(3, 15), # Plage de tailles personnalisées
breaks = c(1, 10, 50, 75), # Points de rupture pour les étiquettes de taille
labels = c("1%", "10%", "50%", "75%") # Étiquettes pour les tailles
) +
# Personnalisez les légendes
labs(
x = "",
y = "",
size = "Abondance relative (%)",
fill = "Valeur",
color = "Valeur"
) +
# Personnalisez les thèmes pour une meilleure lisibilité
theme_minimal() +
# Personnalisez la palette de couleurs
scale_fill_gradient(low = "#F1EEF6", high = "#54278F") +
scale_color_gradient(low = "#F1EEF6", high = "#54278F")
bubble_plot
Interactive tables are useful for large datasets that require filtering or detailed inspection.
The DT package in R makes it easy to create tables that users can sort, search, and explore.
library(reactable)
library(dplyr)
# Create a summary of grouped data
GROUP <- Outcome %>%
group_by(System_C) %>%
summarize(Number = n(), .groups = "drop")
reactable(
GROUP,
details = function(index) {
# Filter the details for the selected group
details_data <- filter(Outcome, System_C == GROUP$System_C[index])
# Create a reactable for the detailed view
tbl <- reactable(details_data,
columns = list(
System_C = colDef(
style = function(value) {
if (value > 0) {
color <- "#008000"
} else if (value < 0) {
color <- "#e00000"
} else {
color <- "#777"
}
list(color = color, fontWeight = "bold")
}
)
))
htmltools::div(style = list(margin = "12px 45px"), tbl)
},
onClick = "expand", # Set click behavior to expand row details
rowStyle = list(cursor = "pointer") # Change cursor style to pointer for rows
)